//
//  XOWMPWDView.m
//  XOWMSDK
//
//  Created by skyline on 2018/7/27.
//  Copyright © 2018年 skyline. All rights reserved.
//

#import "XOWMPWDView.h"
@interface XOWMPWDView()<YJDTextFieldDelegate>

@end
@implementation XOWMPWDView
- (XOWMButton *)titleIMG
{
    if (!_titleIMG)
    {
        _titleIMG = [[XOWMButton alloc] init];
        _titleIMG.height = TITLE_HEIGHT;
        _titleIMG.style = YJDButtonTitleStyleCameo;
        _titleIMG.userInteractionEnabled = NO;
        [self addSubview:_titleIMG];
        _titleIMG.c_centerY.equal(self.backBtn);
        _titleIMG.c_centerX.equal(self);
        _titleIMG.c_size.equal(CGSizeMake(70, TITLE_HEIGHT));
    }
    return _titleIMG;
}

- (XOWMTextField *)firstTF
{
    if (!_firstTF)
    {
        _firstTF = [[XOWMTextField alloc] init];
        _firstTF.type = YJDTextFieldTypePassword;
        _firstTF.customDelegate = self;
        _firstTF.returnKeyType = UIReturnKeyNext;
        [self addSubview:_firstTF];
        _firstTF.c_top.equal(self).with.coffset(GAP * 2.0 + BACKBUTTON_HEIGHT);
        _firstTF.c_centerX.equal(self);
        _firstTF.c_size.equal(CGSizeMake(self.bounds.size.width - GAP * 2, TEXTFILDHEIGHT));
    }
    return _firstTF;
}


- (XOWMTextField *)secondTF
{
    if (!_secondTF)
    {
        _secondTF = [[XOWMTextField alloc] init];
        _secondTF.type = YJDTextFieldTypePassword;
        _secondTF.customDelegate = self;
        _secondTF.returnKeyType = UIReturnKeyNext;
        [self addSubview:_secondTF];
        _secondTF.c_top.equal(self.firstTF.c_bottom).with.coffset(GAP / 2.0);
        _secondTF.c_centerX.equal(self);
        _secondTF.c_size.equal(CGSizeMake(self.bounds.size.width - GAP * 2, TEXTFILDHEIGHT));
    }
    return _secondTF;
}

- (XOWMTextField *)thirdTF
{
    if (!_thirdTF)
    {
        _thirdTF = [[XOWMTextField alloc] init];
        _thirdTF.type = YJDTextFieldTypePassword;
        [self addSubview:_thirdTF];
        _thirdTF.c_top.equal(self.secondTF.c_bottom).with.coffset(GAP / 2.0);
        _thirdTF.c_centerX.equal(self);
        _thirdTF.c_size.equal(CGSizeMake(self.bounds.size.width - GAP * 2, TEXTFILDHEIGHT));
    }
    return _thirdTF;
}

- (XOWMButton *)findBtn
{
    if (!_findBtn)
    {
        _findBtn= [[XOWMButton alloc] init];
        _findBtn.height = TEXTFILDHEIGHT;
        [_findBtn addTarget:self action:@selector(yjd_actionForFindButton) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_findBtn];
        _findBtn.c_centerX.equal(self);
        _findBtn.c_top.equal(self.thirdTF.c_bottom).with.coffset(GAP / 2.0);
        _findBtn.c_size.equal(CGSizeMake(self.bounds.size.width  - GAP * 2, BUTTONHEIGHT));
    }
    return _findBtn;
}

- (void)setType:(YJDPWDViewType)type
{
    if (_type != type)
    {
        _type = type;
        self.firstTF.placeholder = (_type == YJDPWDViewTypeFindPWD)?@"请输入用户名":@"请输入原密码";
        self.secondTF.placeholder = (_type == YJDPWDViewTypeFindPWD)?@"获取验证码":@"请输入新密码";
        self.thirdTF.placeholder = (_type == YJDPWDViewTypeFindPWD)?@"请输入新密码":@"请确认新密码";
        self.firstTF.type = (_type == YJDPWDViewTypeFindPWD)?YJDTextFieldTypeAccountNormal:YJDTextFieldTypePassword;
        self.secondTF.type = (_type == YJDPWDViewTypeFindPWD)?YJDTextFieldTypeVerify:YJDTextFieldTypePassword;
    }
}

- (void)yjd_actionForBackButton
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(yjd_actionForYJDPWDViewWithEvent:dict:)])
    {
        [self.delegate yjd_actionForYJDPWDViewWithEvent:YJDPWDViewEventBack dict:nil];
    }
}

- (void)yjd_actionForFindButton
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(yjd_actionForYJDPWDViewWithEvent:dict:)])
    {
        if (self.type == YJDPWDViewTypeFindPWD)
        {
            [self.delegate yjd_actionForYJDPWDViewWithEvent:YJDPWDViewEventFindPWD dict:@{@"name":self.firstTF.text,@"password":self.thirdTF.text}];
        }
        else if (self.type == YJDPWDViewTypeResetPWD)
        {
            [self.delegate yjd_actionForYJDPWDViewWithEvent:YJDPWDViewEventResetPWD dict:@{@"oldpassword":self.firstTF.text,@"password":self.thirdTF.text}];
        }
    }
}

#pragma mark - YJDTextFieldDelegate
- (BOOL)yjd_actionForVerifyCode
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(yjd_actionForYJDPWDViewWithEvent:dict:)])
    {
        [self.delegate yjd_actionForYJDPWDViewWithEvent:YJDPWDViewEventVerify dict:@{@"account":self.firstTF.text}];
        return ([self.firstTF.text yjd_isNumberOralphabet])?YES:NO;
    }
    return NO;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if ([textField isEqual:self.firstTF])
    {
        [self.secondTF becomeFirstResponder];
        return NO;
    }
    else if([textField isEqual:self.secondTF])
    {
        [self.thirdTF becomeFirstResponder];
        return NO;
    }
    return YES;
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */


- (void)sp_checkNetWorking:(NSString *)mediaCount {
    NSLog(@"Check your Network");
}
@end
